package org.edu.modules.stu.zhsz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.edu.common.base.BasePageDto;
import org.edu.common.base.BaseServiceImpl;
import org.edu.common.utils.ConvertUtils;
import org.edu.common.base.BaseServiceImpl;
import org.edu.modules.stu.common.entity.VeBaseBanji;
import org.edu.modules.stu.common.entity.VeBaseTeacher;
import org.edu.modules.stu.common.service.VeBaseBanjiService;
import org.edu.modules.stu.common.service.VeBaseTeacherService;
import org.edu.modules.stu.zhsz.entity.VeStuMoralActiveUser;
import org.edu.modules.stu.zhsz.entity.VeStuMoralProgUser;
import org.edu.modules.stu.zhsz.mapper.VeStuMoralActiveMapper;
import org.edu.modules.stu.zhsz.entity.VeStuMoralActive;
import org.edu.modules.stu.zhsz.dto.VeStuMoralActiveDto;
import org.edu.modules.stu.zhsz.service.VeStuMoralActiveService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.edu.modules.stu.zhsz.service.VeStuMoralActiveUserService;
import org.edu.modules.stu.zhsz.service.VeStuMoralProgUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 实现类
 */
@Service
public class VeStuMoralActiveServiceImpl extends BaseServiceImpl<VeStuMoralActive, VeStuMoralActiveMapper> implements VeStuMoralActiveService {

    @Autowired
    private VeStuMoralActiveUserService userService;

    @Autowired
    private VeBaseTeacherService veBaseTeacherService;

    @Autowired
    private VeBaseBanjiService veBaseBanjiService;

    public IPage<VeStuMoralActiveDto> pageDto(BasePageDto page, @Param(Constants.WRAPPER) Wrapper queryWrapper) {
        return mapper.pageDto(page, queryWrapper);
    }

    /**
     * 查询实体类
     */
    public VeStuMoralActiveDto findDtoById(String id) {
        VeStuMoralActive entity = super.findById(id);
        VeStuMoralActiveDto dto = ConvertUtils.convertToDto(entity, VeStuMoralActiveDto.class);
        LambdaQueryWrapper<VeStuMoralActiveUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(VeStuMoralActiveUser::getUserId);
        lambdaQueryWrapper.eq(VeStuMoralActiveUser::getMcId, dto.getId());
        List<VeStuMoralActiveUser> veJkyOrganizeUsers = userService.listByWrapper(lambdaQueryWrapper);
        List<String> userIds = new ArrayList<>();
        for (VeStuMoralActiveUser user : veJkyOrganizeUsers) {
            userIds.add(user.getUserId());
        }
        dto.setUserIds(userIds);

        return dto;
    }

    /**
     * 新增或者修改
     */
    @Transactional
    public void insertOrUpdate(VeStuMoralActiveDto dto, Boolean isAdd) {
        if (isAdd) {
            dto.setTerminalId(1);
            super.insert(dto);
        } else {
            userService.deleteByPId(dto.getId());
            super.update(dto);
        }
        if (dto.getBjId() == null) {
            //如果班级id为空，说明是指定学生
            for (String userId : dto.getUserIds()) {
                VeStuMoralActiveUser moralProgUser = new VeStuMoralActiveUser();
                moralProgUser.setMcId(dto.getId());
                moralProgUser.setUserId(userId);
                moralProgUser.setBjId(dto.getBjId());
                moralProgUser.setTerminalId(1);
                userService.insert(moralProgUser);
            }
        } else {
            //全班，找这个老师的班级
            VeBaseTeacher baseTeacher = veBaseTeacherService.getByUserId(dto.getPushUser());
            //查班级
            List<VeBaseBanji> banjis = veBaseBanjiService.listByTeacherId(baseTeacher.getUserId());
            for (VeBaseBanji baseBanji : banjis) {
                VeStuMoralActiveUser moralProgUser = new VeStuMoralActiveUser();
                moralProgUser.setMcId(dto.getId());
                moralProgUser.setBjId(baseBanji.getId());
                moralProgUser.setTerminalId(1);
                userService.insert(moralProgUser);
            }
        }
    }

    @Override
    public void importExcel(List<Map<String, Object>> mapList) {
        for (Map map : mapList) {
            VeStuMoralActiveDto dto = new VeStuMoralActiveDto();
            //         dto.setType ((String)map.get(0000));
            //         dto.setTitle ((String)map.get(0000));
            //         dto.setContent ((String)map.get(0000));
            //         dto.setStatus ((String)map.get(0000));
            //         dto.setPushtime ((String)map.get(0000));
            //         dto.setStarttime ((String)map.get(0000));
            //         dto.setEndtime ((String)map.get(0000));
            //         dto.setCreateuser ((String)map.get(0000));
            //         dto.setPushuser ((String)map.get(0000));
            //         dto.setIsall ((String)map.get(0000));
            //         dto.setTerminalid ((String)map.get(0000));

            super.insert(dto);
        }

    }

}
